package com.burstly.lib.component.networkcomponent.burstly.html.ormma;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.webkit.JavascriptInterface;
import com.burstly.lib.component.networkcomponent.burstly.html.ormma.listener.LocListener;
import com.burstly.lib.util.LoggerExt;
import java.util.Iterator;
import java.util.List;

public class OrmmaLocationController extends OrmmaController
{
  private static final int INTERVAL = 1000;
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = "OrmmaLocationController";
  private boolean mAllowLocationServices;
  private LocListener mGps;
  private boolean mHasPermission;
  private int mLocListenerCount;
  private LocationManager mLocationManager;
  private LocListener mNetwork;

  public OrmmaLocationController(OrmmaView paramOrmmaView, Context paramContext)
  {
    super(paramOrmmaView, paramContext);
    this.mLocationManager = ((LocationManager)paramContext.getSystemService("location"));
    this.mHasPermission = true;
    getGpsProvider(paramContext);
    getNetworkProvider(paramContext);
  }

  private static String formatLocation(Location paramLocation)
  {
    return "{ lat: " + paramLocation.getLatitude() + ", lon: " + paramLocation.getLongitude() + ", acc: " + paramLocation.getAccuracy() + "}";
  }

  private void getGpsProvider(Context paramContext)
  {
    try
    {
      if (this.mLocationManager.getProvider("gps") != null)
        this.mGps = new LocListener(paramContext, 1000, this, "gps");
      return;
    }
    catch (SecurityException localSecurityException)
    {
      this.mHasPermission = false;
      LOG.logWarning("OrmmaLocationController", "Manifest file does not have android.permission.ACCESS_FINE_LOCATION so GPS will not be available for ORMMA creatives", new Object[0]);
    }
  }

  private void getNetworkProvider(Context paramContext)
  {
    try
    {
      if (this.mLocationManager.getProvider("network") != null)
        this.mNetwork = new LocListener(paramContext, 1000, this, "network");
      return;
    }
    catch (SecurityException localSecurityException)
    {
      this.mHasPermission = false;
      LOG.logWarning("OrmmaLocationController", "Manifest file has neither Requires either of the permissions android.permission.ACCESS_COARSE_LOCATIONnor android.permission.ACCESS_FINE_LOCATION. so no network location will be available for ORMMA creatives", new Object[0]);
    }
  }

  @JavascriptInterface
  public void allowLocationServices(boolean paramBoolean)
  {
    this.mAllowLocationServices = paramBoolean;
  }

  public boolean allowLocationServices()
  {
    return this.mAllowLocationServices;
  }

  public void fail()
  {
    LOG.logError("OrmmaLocationController", "Location can't be determined", new Object[0]);
    this.mOrmmaView.injectJavaScript("window.ormmaview.fireErrorEvent(\"Location cannot be identified\", \"OrmmaLocationController\")");
  }

  @JavascriptInterface
  public String getLocation()
  {
    LoggerExt localLoggerExt = LOG;
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = Boolean.valueOf(this.mHasPermission);
    localLoggerExt.logDebug("OrmmaLocationController", "getLocation: mHasPermission: {0}", arrayOfObject);
    if (!this.mHasPermission)
      return null;
    Iterator localIterator = this.mLocationManager.getProviders(true).iterator();
    Location localLocation = null;
    do
    {
      if (!localIterator.hasNext())
        break;
      localLocation = this.mLocationManager.getLastKnownLocation((String)localIterator.next());
    }
    while (localLocation == null);
    LOG.logDebug("OrmmaLocationController", "getLocation: {0}", new Object[] { localLocation });
    if (localLocation != null)
      return formatLocation(localLocation);
    return null;
  }

  public void startLocationListener()
  {
    if (this.mLocListenerCount == 0)
    {
      if (this.mNetwork != null)
        this.mNetwork.start();
      if (this.mGps != null)
        this.mGps.start();
    }
    this.mLocListenerCount = (1 + this.mLocListenerCount);
  }

  public void stopAllListeners()
  {
    this.mLocListenerCount = 0;
    try
    {
      if (this.mGps != null)
        this.mGps.stop();
    }
    catch (Exception localException1)
    {
      try
      {
        while (true)
        {
          if (this.mNetwork != null)
            this.mNetwork.stop();
          return;
          localException1 = localException1;
          LOG.logThrowable("OrmmaLocationController", localException1);
        }
      }
      catch (Exception localException2)
      {
        LOG.logThrowable("OrmmaLocationController", localException2);
      }
    }
  }

  @JavascriptInterface
  public void stopLocationListener()
  {
    this.mLocListenerCount = (-1 + this.mLocListenerCount);
    if (this.mLocListenerCount == 0)
    {
      if (this.mNetwork != null)
        this.mNetwork.stop();
      if (this.mGps != null)
        this.mGps.stop();
    }
  }

  public void success(Location paramLocation)
  {
    String str = "window.ormmaview.fireChangeEvent({ location: " + formatLocation(paramLocation) + "})";
    LOG.logDebug("OrmmaLocationController", str, new Object[0]);
    this.mOrmmaView.injectJavaScript(str);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.component.networkcomponent.burstly.html.ormma.OrmmaLocationController
 * JD-Core Version:    0.6.2
 */